package util;

/**
 * @Author: WangYongGan
 * @Date: 2021/11/2 13:31
 * @Description:
 */
import java.net.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class URLConnDemo
{
    public static void main(String [] args)
    {
        try
        {
            URL url = new URL("http://www.runoob.com");
            System.out.println(url.getUserInfo());

            URLConnection urlConnection = url.openConnection();
            System.out.println(urlConnection.getContent());
            System.out.println(urlConnection.getContentEncoding());
            System.out.println(urlConnection.getContentLength());
            System.out.println(urlConnection.getContentType());
            HttpURLConnection connection = null;
            if(urlConnection instanceof HttpURLConnection)
            {
                connection = (HttpURLConnection) urlConnection;
            }
            else
            {
                System.out.println("请输入 URL 地址");
                return;
            }
//            connection.getOutputStream().write();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String urlString = "";
            String current;
            while((current = in.readLine()) != null)
            {
                urlString += current;
            }
            System.out.println(urlString);
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}
